package com.github.niefy.modules.third.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.third.dto.out.ThirdHTMLOut_001;
import com.github.niefy.modules.third.enums.SpmApiidEnum;
import com.github.niefy.modules.third.enums.SecApiURLEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * spm HTML转发
 */
@Controller
@RequestMapping("/third/thirdHTML")
@Tag(name = "第三方HTML转发")
@RequiredArgsConstructor
@Slf4j
public class ThirdSpmHTMLController {


    @Value("${sec-api.spm.domain}")
    String spmDomain;
    @Value("${sec-api.spm.accesskey}")
    String accesskey;

    private final WxMpService wxMpService;

    /**
     * 查询学生学业进展报告[当前]
     * @param request
     * @param response
     * @return
     * @throws WxErrorException
     */
    @GetMapping(value = "/getSpmCurrTermRpt")
    @ResponseBody
    @Operation(summary = "查询学生学业进展报告[当前]", description = "")
    public R getSpmCurrTermRpt(HttpServletRequest request, HttpServletResponse response,
                               @RequestAttribute String thirdId) throws WxErrorException, IOException {
        String url = spmDomain + SecApiURLEnum.SPM_CALL;
        var reqOut = ThirdHTMLOut_001.builder()
                .studentno(thirdId)
                .build();
        reqOut.createSign(SpmApiidEnum.queryspmcurrtermrpt.name(),  "SPMSTU",
                "studentno", accesskey, thirdId);
        url = HttpUtil.urlWithForm(url, BeanUtil.beanToMap(reqOut), StandardCharsets.UTF_8, false);
        return R.ok().put("url",url);
    }

    /**
     * 查询学生专业能力达成度报告
     * @param request
     * @param response
     * @return
     * @throws WxErrorException
     */
    @GetMapping(value = "/getStudentAbltShow")
    @ResponseBody
    @Operation(summary = "查询学生专业能力达成度报告", description = "")
    public R getStudentAbltShow(HttpServletRequest request, HttpServletResponse response,
                                  @RequestAttribute String thirdId) throws WxErrorException, IOException {
        String url = spmDomain + SecApiURLEnum.SPM_CALL;
        var reqOut = ThirdHTMLOut_001.builder()
                .studentno(thirdId)
                .build();
        reqOut.createSign(SpmApiidEnum.querystudentabltshow.name(),  "SPMSTU",
                "studentno", accesskey, thirdId);
        url = HttpUtil.urlWithForm(url, BeanUtil.beanToMap(reqOut), StandardCharsets.UTF_8, false);
        return R.ok().put("url",url);
    }
}
